<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of DepartamentoDaoImp
 *
 * @author Rafael
 */
//require_once("interfaces/DepartamentoDao.php");

class DepartamentoDaoImp {

    //armazena a conex�o com o BD
    private $conn;

    public function __construct() {
        require_once("Conexao.class.php");
        $funcCon = new Conexao;
        $this->conn = $funcCon->conectar();
    }

    public function inserir($dep) {
        try {
            //cria a conta
            $result = $this->conn->exec("Insert into conta values(default,0)");

            $query = $this->conn->query("Select * from conta order by idConta desc");
            $x = $query->fetch(PDO::FETCH_OBJ);

           
            if ($result) {
                
                $result = $this->conn->exec("Insert into departamento values(default,{$x->idConta},{$dep->getDepSuperior()}, '{$dep->getNome()}','{$dep->getSigla()}', {$dep->getSituacao()})");
            }
        } catch (PDOException $e) {
            print "Erro!: " . $e->getMessage() . "\n";
            die();
        } catch (Exception $e) {
            print "Erro: " . $e->getMessage() . "\n";
        }

        return $result;
    }

    public function fechar($id) {
        try {
            //Executa a instrua��o sql
            $result = $this->conn->exec("Update departamento set situacao = 0  where idDepartamento = {$id}");
        } catch (PDOException $e) {
            print "Erro!: " . $e->getMessage() . "\n";
            die();
        }

        return $result;
    }

    public function alterar($dep) {
        try {
            //Executa a instrua��o sql
            
            $result = $this->conn->exec("Update departamento set conta = {$dep->getConta()}, nome ='{$dep->getNome()}', sigla = '{$dep->getSigla()}', depSuperior = {$dep->getDepSuperior()}, situacao = {$dep->getSituacao()} where idDepartamento = {$dep->getId()}");
        } catch (PDOException $e) {
            print "Erro!: " . $e->getMessage() . "\n";
            die();
        }
        return $result;
    }

    public function buscarNome($nome) {
        try {
            //Executa a instrua��o sql
            $result = $this->conn->query("Select * from departamento where nome like '%{$nome}%' and situacao = 1");
        } catch (PDOException $e) {
            print "Erro!: " . $e->getMessage() . "\n";
            die();
        }
        return $result;
    }

    public function buscarId($id) {
        try {
            //Executa a instrua��o sql
            $result = $this->conn->query("Select * from departamento where idDepartamento = {$id} and situacao = 1");
        } catch (PDOException $e) {
            print "Erro!: " . $e->getMessage() . "\n";
            die();
        }
        return $result;
    }

    public function buscarDepSuperior($id) {
        try {
            //Executa a instrua��o sql
            $result = $this->conn->query("Select d1.nome, d2.nome as nomeDepSuperior from departamento d1 inner join departamento d2 where d1.idDepartamento = {$id} AND d1.depSuperior = d2.idDepartamento");
        } catch (PDOException $e) {
            print "Erro!: " . $e->getMessage() . "\n";
            die();
        }
        return $result;
    }

    public function buscarTodos() {
        try {
            //Executa a instrua��o sql
            $result = $this->conn->query("Select * from departamento");
        } catch (PDOException $e) {
            print "Erro!: " . $e->getMessage() . "\n";
            die();
        }
        return $result;
    }

    public function __destruct() {
        //fecha a conex�o
        $this->conn = null;
    }

}
?>
